﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Messaging;

namespace MSMQWinform
{
    public partial class Main : Form
    {
        private string m_InstanceQueuePath = string.Empty;
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            try
            {
                QueueManager.CreateQueue(txt_queueName.Text.Trim(), checkBox_isFormat.Checked);
                MessageBox.Show("Queue [" + txt_queueName.Text.Trim() + "] Create Successful !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            m_InstanceQueuePath = txt_connect.Text.Trim();
            bool isExists = QueueManager.CheckQueueExists(m_InstanceQueuePath, checkBox_isFormat.Checked);
            groupBox_send.Enabled = groupBox_receive.Enabled = isExists;

            if (isExists)
            {
                txt_sendData.Text = DateTime.Now.ToString();
                StartListen();
            }
        }

        private void btn_sendMsg_Click(object sender, EventArgs e)
        {
            SendMsg m_Msg = new SendMsg() { Sender = txt_sender.Text.Trim(), SendData = DateTime.Parse(txt_sendData.Text.Trim()), SendMessage = txt_sendMsg.Text.Trim() };
            try
            {
                System.Diagnostics.Stopwatch m_Stopwatch = new System.Diagnostics.Stopwatch();
                m_Stopwatch.Start();

                int i = 20;
                while (i-- > 0)
                {
                    QueueManager.SendMsg<SendMsg>(m_InstanceQueuePath, m_Msg,checkBox_isFormat.Checked);
                }
                m_Stopwatch.Stop();

                MessageBox.Show("Send 20 Message Use " + m_Stopwatch.ElapsedMilliseconds + " MilliSeconds !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void StartListen()
        {
            ThreadManager.Instance.QueuePath = m_InstanceQueuePath;
            ThreadManager.Instance.MessageDialog = MessageDialogShow;
            ThreadManager.Instance.ReceiveMessage = ReceiveMessageShow;

            ThreadManager.Instance.ThreadStart();
        }

        private void MessageDialogShow(string msg)
        {
            if (listBox_listen.InvokeRequired)
            {
                MessageDialog m_MessageDialog = new MessageDialog(MessageDialogShow);
                this.Invoke(m_MessageDialog, new object[] { msg });
            }
            else
            {
                listBox_listen.Items.Add(msg);
                listBox_listen.SetSelected(listBox_listen.Items.Count - 1, true);
            }
        }
        private void ReceiveMessageShow(Object obj)
        {
            if (obj == null) return;
            try
            {

                if (listBox_Msg.InvokeRequired)
                {
                    ReceiveMessage m_ReceiveMessage = new ReceiveMessage(ReceiveMessageShow);
                    this.Invoke(m_ReceiveMessage, new object[] { obj });
                }
                else
                {
                    System.Messaging.Message m_Message = (System.Messaging.Message)obj;
                    SendMsg m_ReceiveMsg = (SendMsg)m_Message.Body;
                    string m_Item = string.Format("Sender:{0},SendData:{1},SendMsg:{2} ", m_ReceiveMsg.Sender, m_ReceiveMsg.SendData, m_ReceiveMsg.SendMessage);
                    listBox_Msg.Items.Add(m_Item);
                    listBox_Msg.SetSelected(listBox_Msg.Items.Count - 1, true);
                }
            }
            catch
            { }
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            ThreadManager.Instance.ThreadStop();
        }

        private void btn_clearReceive_Click(object sender, EventArgs e)
        {
            listBox_Msg.Items.Clear();
        }
    }
}
